﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bluyel.Core
{
    /// <summary>
    /// A Type LOV with item in basic type, 
    /// type of values can be: string, int, double, bool.
    /// 
    /// TODO: si item de type string, associer item à une valeur entière, ou bool si LOV à 2 valeurs (comme une LOV de type Bool).
    /// </summary>
    public abstract class TypeEnumBasic : ITypeEnumBasic
    {
        UniqueId _id;

        string _name;

        // type of items
        ITypeBasic _typeItem;

        TypeEnumSelectMode _selectMode;

        // items of the LOV
        List<IValue> _listItems = new List<IValue>();

        //=====================================================================
        #region Constructor.

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public TypeEnumBasic(UniqueId id, string name, ITypeBasic typeItem, TypeEnumSelectMode selectMode)
        {
            _id = id;
            _name = name;
            _typeItem = typeItem;
            _selectMode = selectMode;
        }
        #endregion

        //=====================================================================
        #region Properties.

        //---------------------------------------------------------------------
        public UniqueId Id 
        {
            get { return _id; } 
        }

        //---------------------------------------------------------------------
        public string Name 
        {
            get { return _name; } 
        }

        //---------------------------------------------------------------------
        // Type of values
        public IType TypeValue
        { get { return _typeItem; } }

        //---------------------------------------------------------------------
        public TypeEnumSelectMode SelectMode
        { get { return _selectMode;  } }

        //---------------------------------------------------------------------
        public int ValuesCount
        { get { return _listItems.Count; } }

        //---------------------------------------------------------------------
        // items of the LOV
        public IEnumerable<IValue> ListItems 
        {
            get { return _listItems; } 
        }

        //---------------------------------------------------------------------
        //public DataValueString GetDataValueString(string value)
        //{
        //    if(string.IsNullOrWhiteSpace(value))
        //        return null;

        //    _listItems.Find(d=>d.)
        //}

        #endregion

        //=====================================================================
        #region Public methods.

        //---------------------------------------------------------------------
        public bool Contains(IValue dvValue)
        {
            if (dvValue == null)
                return false;

            return _listItems.Contains(dvValue);
        }

        //---------------------------------------------------------------------
        public IValue GetAt(int pos)
        {
            if (pos < 0 || pos >= _listItems.Count)
                return null;

            return _listItems[pos];
        }

        //---------------------------------------------------------------------
        public bool AddItem(IValue value)
        {
            if (value == null)
                return false;

            // check that the dataValue has the defined type
            // TODO:  comment, les fct sont dans typeFinder!

            _listItems.Add(value);
            return true;
        }

        //---------------------------------------------------------------------
        // remove a value, by pos
        public bool RemoveByPos(int pos)
        {
            if (pos < 0 || pos >= _listItems.Count)
                return false;

            _listItems.RemoveAt(pos);
            return true;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Remove a value, by the instance.
        /// </summary>
        /// <param name="dataValue"></param>
        /// <returns></returns>
        public bool Remove(IValue dataValue)
        {
            if (dataValue == null)
                return false;

            if (!_listItems.Contains(dataValue))
                return false;

            _listItems.Remove(dataValue);
            return true;
        }

        #endregion


    }
}
